<?php

namespace app\common\lib;

use think\facade\Db;

class MakeCompose
{
    public $xiangmu = '';
    public $dalei = '';
    public $xiaolei = '';  
    public $keywords = '';
    public $keywords_type = '';

    public function run(){
        $this->com_name = '';
        $this->com_regdate = '';
        $this->com_address = '';
        $this->parent_typeid = 0;
        $this->typeid = 0;
        $this->section_vice = '';

        if(!empty($this->xiangmu)){
            $data['project'] = $this->xiangmu;
            $pro = Db::name('data_project')->field('company,typeid,parent_typeid')->where('title',$this->xiangmu)->find();
            if(!empty($pro)){
                $this->com_name = $pro['company'];
                $this->typeid = $pro['typeid'];
                $this->parent_typeid = $pro['parent_typeid'];
            }
        }
        if(!empty($this->com_name)){
            $com = Db::name('data_company')->field('title,regDate,address')->where('title',$this->com_name)->find();
            if(!empty($com)){
                $this->com_regdate = $com['regDate'];
                $this->com_address = $com['address'];
            }
        }
        $data['title'] = $this->getTitle();
        $data['content'] = $this->getContent();
        $data['keywords'] = $this->keywords;
        $data['typeid'] = $this->typeid;
        $data['parent_typeid'] = $this->parent_typeid;
        $data['section_main'] = $this->keywords_type;
        $data['section_vice'] = ltrim($this->section_vice,',');
        $data['attr'] = getAttr($data['title']);
        return $data;
    }

    private function getTitle(){
        $title = Db::name('data_text_title')->field('content')->orderRaw('rand()')->find();
        return $this->keywords.$this->replaceTag($title['content']);
    }

    private function getContent(){
        $start = Db::name('data_text_start')->field('id,content')->where('num','<',3)->orderRaw('rand()')->find();
        $end = Db::name('data_text_end')->field('id,content')->where('num','<',10)->orderRaw('rand()')->find();
        if(!empty($start)){
            $content .= '<p>'.$start['content'].'</p>';
            Db::name('data_text_start')->where('id',$start['id'])->inc('num')->update();
        }
        $content .= $this->getPic(1);
        $content .= $this->getMainSection();
        $arr = range(0,6);
        shuffle($arr);
        for($i=0;$i<mt_rand(2,4);$i++){
            $content .= $this->getViceSection($arr[$i]);
            if($i==0){
                $content .= $this->getPic(mt_rand(1,2));
            }
        }
        $content .= $this->getPic(2);
        if(!empty($end)){
            $content .= '<p>'.$end['content'].'</p>';
            Db::name('data_text_end')->where('id',$end['id'])->inc('num')->update();
        }
        $content = $this->replaceTag($content);
        return $content;
    }

    private function getPic($flag){
        $data = '';
        switch ($flag) {
            case 1:
                $res = Db::name('data_pic_project')->field('url')->where('project',$this->xiangmu)->orderRaw('rand()')->find();
                if(empty($res)){
                    $res = Db::name('data_pic_type')->field('url')->where('typeid',$this->typeid)->orderRaw('rand()')->find();
                }
                if(!empty($res)){
                    $data = '<p style="text-align: center;"><img src="'.$res['url'].'" /></p>';
                }
            break;
            
            case 2:
                $res = Db::name('data_pic_type')->field('url')->where('typeid',$this->typeid)->orderRaw('rand()')->find();
                if(!empty($res)){
                    $data = '<p style="text-align: center;"><img src="'.$res['url'].'" /></p>';
                }
            break; 
        }
        return $data;
    }

    private function getMainSection(){
        $data = '';
        switch ($this->keywords_type) {
            case '费用':
                $make = new \app\common\lib\MakeTable();
                $data = $make->run($this->dalei,'cost');
                if(!empty($data)){
                    $data = '<h2>'.$this->xiangmu.'加盟费用</h2>'.$data;
                }
            break;
            case '利润':
                $make = new \app\common\lib\MakeTable();
                $data = $make->run($this->dalei,'profit');
                if(!empty($data)){
                    $data = '<h2>'.$this->xiangmu.'加盟利润</h2>'.$data;
                }
            break;

            case '条件':
                $data = $this->getChip(1);
                if(!empty($data)){
                    $data = '<h2>'.$this->xiangmu.'加盟条件</h2>'.$data;
                }
            break;
            case '优势':
                $data = $this->getChip(2);
                if(!empty($data)){
                    $data = '<h2>'.$this->xiangmu.'加盟优势</h2>'.$data;
                }
            break;
            case '流程':
                $data = $this->getChip(3);
                if(!empty($data)){
                    $data = '<h2>'.$this->xiangmu.'加盟流程</h2>'.$data;
                }
            break;
            case '支持':
                $data = $this->getChip(4);
                if(!empty($data)){
                    $data = '<h2>'.$this->xiangmu.'加盟支持</h2>'.$data;
                }
            break;

            case '靠谱':
                $section = Db::name('data_text_section')->field('id,content')->where([
                    ['position','=',1],
                    ['num','<',3]
                ])->orderRaw('rand()')->find();
                if(!empty($section)){
                    $data = $this->getSectionTitle(1).'<p>'.$section['content'].'</p>';
                    Db::name('data_text_section')->where('id',$section['id'])->inc('num')->update();
                }
            break;
            case '真假':
                $section = Db::name('data_text_section')->field('id,content')->where([
                    ['position','=',2],
                    ['num','<',3]
                ])->orderRaw('rand()')->find();
                if(!empty($section)){
                    $data = $this->getSectionTitle(2).'<p>'.$section['content'].'</p>';
                    Db::name('data_text_section')->where('id',$section['id'])->inc('num')->update();
                }
            break;
            case '骗局':
                $section = Db::name('data_text_section')->field('id,content')->where([
                    ['position','=',3],
                    ['num','<',3]
                ])->orderRaw('rand()')->find();
                if(!empty($section)){
                    $data = $this->getSectionTitle(3).'<p>'.$section['content'].'</p>';
                    Db::name('data_text_section')->where('id',$section['id'])->inc('num')->update();
                }
            break;
            case '前景':
                $section = Db::name('data_text_section')->field('id,content')->where([
                    ['position','=',4],
                    ['num','<',3]
                ])->orderRaw('rand()')->find();
                if(!empty($section)){
                    $data = $this->getSectionTitle(4).'<p>'.$section['content'].'</p>';
                    Db::name('data_text_section')->where('id',$section['id'])->inc('num')->update();
                }
            break;
            case '生意':
                $section = Db::name('data_text_section')->field('id,content')->where([
                    ['position','=',5],
                    ['num','<',3]
                ])->orderRaw('rand()')->find();
                if(!empty($section)){
                    $data = $this->getSectionTitle(5).'<p>'.$section['content'].'</p>';
                    Db::name('data_text_section')->where('id',$section['id'])->inc('num')->update();
                }
            break;
            case '赔亏':
                $section = Db::name('data_text_section')->field('id,content')->where([
                    ['position','=',6],
                    ['num','<',3]
                ])->orderRaw('rand()')->find();
                if(!empty($section)){
                    $data = $this->getSectionTitle(6).'<p>'.$section['content'].'</p>';
                    Db::name('data_text_section')->where('id',$section['id'])->inc('num')->update();
                }
            break;
            case '怎么样':
                $section = Db::name('data_text_section')->field('id,content')->where([
                    ['position','=',7],
                    ['num','<',3]
                ])->orderRaw('rand()')->find();
                if(!empty($section)){
                    $data = $this->getSectionTitle(6).'<p>'.$section['content'].'</p>';
                    Db::name('data_text_section')->where('id',$section['id'])->inc('num')->update();
                }
            break;
            case '好不好':
                $section = Db::name('data_text_section')->field('id,content')->where([
                    ['position','=',8],
                    ['num','<',3]
                ])->orderRaw('rand()')->find();
                if(!empty($section)){
                    $data = $this->getSectionTitle(6).'<p>'.$section['content'].'</p>';
                    Db::name('data_text_section')->where('id',$section['id'])->inc('num')->update();
                }
            break;
            case '值吗':
                $section = Db::name('data_text_section')->field('id,content')->where([
                    ['position','=',9],
                    ['num','<',3]
                ])->orderRaw('rand()')->find();
                if(!empty($section)){
                    $data = $this->getSectionTitle(6).'<p>'.$section['content'].'</p>';
                    Db::name('data_text_section')->where('id',$section['id'])->inc('num')->update();
                }
            break;
            case '赚钱':
                $section = Db::name('data_text_section')->field('id,content')->where([
                    ['position','=',10],
                    ['num','<',3]
                ])->orderRaw('rand()')->find();
                if(!empty($section)){
                    $data = $this->getSectionTitle(6).'<p>'.$section['content'].'</p>';
                    Db::name('data_text_section')->where('id',$section['id'])->inc('num')->update();
                }
            break;
            case '怎么加盟':
                $section = Db::name('data_text_section')->field('id,content')->where([
                    ['position','=',11],
                    ['num','<',3]
                ])->orderRaw('rand()')->find();
                if(!empty($section)){
                    $data = $this->getSectionTitle(6).'<p>'.$section['content'].'</p>';
                    Db::name('data_text_section')->where('id',$section['id'])->inc('num')->update();
                }
            break;
            case '口碑':
                $section = Db::name('data_text_section')->field('id,content')->where([
                    ['position','=',12],
                    ['num','<',3]
                ])->orderRaw('rand()')->find();
                if(!empty($section)){
                    $data = $this->getSectionTitle(6).'<p>'.$section['content'].'</p>';
                    Db::name('data_text_section')->where('id',$section['id'])->inc('num')->update();
                }
            break;
        }
        return $data;
    }

    private function getViceSection($num){
        $data = '';
        switch ($num) {
            case 0:
                $section = Db::name('data_text_section')->field('id,content,position')->where([
                    ['num','>=',3],
                    ['num2','<',3]
                ])->orderRaw('rand()')->find();
                if(!empty($section['content'])){
                    switch ($section['position']) {
                        case 1:
                            if($this->keywords_type != '靠谱'){
                                $data = $this->getSectionTitle($section['position']).'<p>'.$section['content'].'</p>';
                                Db::name('data_text_section')->where('id',$section['id'])->inc('num2')->update();
                                $this->section_vice .= ',靠谱';
                            }
                        break;
                        case 2:
                            if($this->keywords_type != '真假'){
                                $data = $this->getSectionTitle($section['position']).'<p>'.$section['content'].'</p>';
                                Db::name('data_text_section')->where('id',$section['id'])->inc('num2')->update();
                                $this->section_vice .= ',真假';
                            }
                        break;
                        case 3:
                            if($this->keywords_type != '骗局'){
                                $data = $this->getSectionTitle($section['position']).'<p>'.$section['content'].'</p>';
                                Db::name('data_text_section')->where('id',$section['id'])->inc('num2')->update();
                                $this->section_vice .= ',骗局';
                            }
                        break;
                        case 4:
                            if($this->keywords_type != '前景'){
                                $data = $this->getSectionTitle($section['position']).'<p>'.$section['content'].'</p>';
                                Db::name('data_text_section')->where('id',$section['id'])->inc('num2')->update();
                                $this->section_vice .= ',前景';
                            }
                        break;
                        case 5:
                            if($this->keywords_type != '生意'){
                                $data = $this->getSectionTitle($section['position']).'<p>'.$section['content'].'</p>';
                                Db::name('data_text_section')->where('id',$section['id'])->inc('num2')->update();
                                $this->section_vice .= ',生意';
                            }
                        break;
                        case 6:
                            if($this->keywords_type != '赔亏'){
                                $data = $this->getSectionTitle($section['position']).'<p>'.$section['content'].'</p>';
                                Db::name('data_text_section')->where('id',$section['id'])->inc('num2')->update();
                                $this->section_vice .= ',赔亏';
                            }
                        break;
                        case 7:
                            if($this->keywords_type != '怎么样'){
                                $data = $this->getSectionTitle($section['position']).'<p>'.$section['content'].'</p>';
                                Db::name('data_text_section')->where('id',$section['id'])->inc('num2')->update();
                                $this->section_vice .= ',怎么样';
                            }
                        break;
                        case 8:
                            if($this->keywords_type != '好不好'){
                                $data = $this->getSectionTitle($section['position']).'<p>'.$section['content'].'</p>';
                                Db::name('data_text_section')->where('id',$section['id'])->inc('num2')->update();
                                $this->section_vice .= ',好不好';
                            }
                        break;
                        case 9:
                            if($this->keywords_type != '值吗'){
                                $data = $this->getSectionTitle($section['position']).'<p>'.$section['content'].'</p>';
                                Db::name('data_text_section')->where('id',$section['id'])->inc('num2')->update();
                                $this->section_vice .= ',值吗';
                            }
                        break;
                        case 10:
                            if($this->keywords_type != '赚钱'){
                                $data = $this->getSectionTitle($section['position']).'<p>'.$section['content'].'</p>';
                                Db::name('data_text_section')->where('id',$section['id'])->inc('num2')->update();
                                $this->section_vice .= ',赚钱';
                            }
                        break;
                        case 11:
                            if($this->keywords_type != '怎么加盟'){
                                $data = $this->getSectionTitle($section['position']).'<p>'.$section['content'].'</p>';
                                Db::name('data_text_section')->where('id',$section['id'])->inc('num2')->update();
                                $this->section_vice .= ',怎么加盟';
                            }
                        break;
                        case 12:
                            if($this->keywords_type != '口碑'){
                                $data = $this->getSectionTitle($section['position']).'<p>'.$section['content'].'</p>';
                                Db::name('data_text_section')->where('id',$section['id'])->inc('num2')->update();
                                $this->section_vice .= ',口碑';
                            }
                        break;
                    }
                }
                break;
            case 1:
                if($this->keywords_type != '条件'){
                    $data = $this->getChip(1);
                    if(!empty($data)){
                        $data = '<h2>'.$this->xiangmu.'加盟条件</h2>'.$data;
                        $this->section_vice .= ',条件';
                    }
                }
                break;
            case 2:
                if($this->keywords_type != '优势'){
                    $data = $this->getChip(2);
                    if(!empty($data)){
                        $data = '<h2>'.$this->xiangmu.'加盟优势</h2>'.$data;
                        $this->section_vice .= ',优势';
                    }
                }
                break;
            case 3:
                if($this->keywords_type != '流程'){
                    $data = $this->getChip(3);
                    if(!empty($data)){
                        $data = '<h2>'.$this->xiangmu.'加盟流程</h2>'.$data;
                        $this->section_vice .= ',流程';
                    }
                }
                break;
            case 4:
                if($this->keywords_type != '支持'){
                    $data = $this->getChip(4);
                    if(!empty($data)){
                        $data = '<h2>'.$this->xiangmu.'加盟支持</h2>'.$data;
                        $this->section_vice .= ',支持';
                    }
                }
                break;
            case 5:
                if($this->keywords_type != '费用'){
                    $make = new \app\common\lib\MakeTable();
                    $data = $make->run($this->dalei,'cost');
                    if(!empty($data)){
                        $data = '<h2>'.$this->xiangmu.'加盟费用</h2>'.$data;
                        $this->section_vice .= ',费用';
                    }
                }
                break;
            case 6:
                if($this->keywords_type != '利润'){
                    $make = new \app\common\lib\MakeTable();
                    $data = $make->run($this->dalei,'profit');
                    if(!empty($data)){
                        $data = '<h2>'.$this->xiangmu.'加盟利润</h2>'.$data;
                        $this->section_vice .= ',利润';
                    }
                }
                break;
        }
        return $data;
    }

    private function replaceTag($content){
        $arr = [
            '[替换:所属大类]' => $this->dalei,
            '[替换:所属小类]' => $this->xiaolei,
            '[替换:项目名称]' => $this->xiangmu,
            '[替换:公司名称]' => $this->com_name,
            '[替换:公司成立时间]' => $this->com_regdate,
            '[替换:公司地址]' => $this->com_address,
            '[替换:关键词]' => $this->keywords
        ];
        foreach($arr as $k => $v){
            $content = str_replace($k,$v,$content);
        }
        return $content;
    }

    //获取 条件 优势 流程
    private function getChip($position){
        $str = '';
        if($position == 3){
            $tag = 'ol';
        }else{
            $tag = 'ul';
        }
        $make = new \app\common\lib\MakeChip();
        $data = $make->run($position);
        if(!empty($data)){
            $num = 0;
            $count = count($data);
            $str .= '<'.$tag.'>';
            foreach ($data as $v) {
                $num++;
                if($num==$count){
                    $v .= '。';
                }else{
                    $v .= '；';
                }
                $v = str_replace('www', '17商机网', $v);
                $v = str_replace('xxx', $this->xiangmu, $v);
                $v = str_replace('dalei', $this->dalei, $v);
                $v = str_replace('xiaolei', $this->xiaolei, $v);
                $str .= '<li>'.$v.'</li>';
            }
            $str .= '</'.$tag.'>';
        }
        return $str;
    }

    private function getSectionTitle($position){
        $data = '';
        $title = Db::name('data_text_section_title')->field('id,content')->where('position',$position)->orderRaw('rand()')->find();
        if(!empty($title)){
            $data = '<h2>'.$title['content'].'</h2>';
        }
        return $data;
    }

}